using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using SigmaStock_Components;
using System.Data.OleDb;


namespace project
{
    public partial class Mag_Report : Form
    {
        OleDbConnection conn;
        string query;
        public static DateTime report_date;
        public static string report_type;
        public static string report_on;
        public ReportHome rep_home;
        //  static String[] r_type={"daily","monthly"};
        //  static String[] r_on ={ "stock_date", "expenses","transaction" };
        private ArrayList stockValues;
        private ArrayList expenseValues;
        private ArrayList transactionValues;
        private ReportDocument DBObjectsReport;

        public Mag_Report()
        {
            //            magValues = new ArrayList();
            stockValues = new ArrayList();
            expenseValues = new ArrayList();
            transactionValues = new ArrayList();
            Console.WriteLine(report_on);
            InitializeComponent();
            ConfigureCrystalReports();

        }

        private string AddDBValues()
        {
            conn = DBComponent.Mag_DealerDB.dbconnection();
            conn.Open();

            if (report_type.Equals("daily") && report_on.Equals("stock_date"))
            {
                Console.WriteLine(report_date.ToString());
                query = "select MNAME,MDATE,OPENING_STOCK,CLOSING_STOCK from stock_date where SDATE=#" + report_date.ToString() + "#;";
                OleDbCommand command = new OleDbCommand(query, conn);
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    string mname = reader[0].ToString();
                    DateTime curr_Date = DateTime.Parse(reader[1].ToString());
                    int opening_stock = int.Parse(reader[2].ToString());
                    int closing_stcok = int.Parse(reader[3].ToString());

                    StockDate_Class stock = new StockDate_Class(mname, curr_Date, opening_stock, closing_stcok);
                    stockValues.Add(stock);


                }
                return "stock";
            }

            if (report_type.Equals("daily") && report_on.Equals("expenses"))
            {
                Console.WriteLine(report_date.ToString());
                query = "select EXPENSE_TYPE,AMOUNT from expenses where CURR_DATE=#" + report_date.ToString() + "#;";
                OleDbCommand command = new OleDbCommand(query, conn);
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    string exp_type = reader[0].ToString();
                    int amt = int.Parse(reader[1].ToString());

                    Expense_Class stock = new Expense_Class(exp_type, amt);
                    expenseValues.Add(stock);

                }
                return "expense";
            }

            if (report_type.Equals("daily") && report_on.Equals("transaction"))
            {
                Console.WriteLine(report_date.ToString());
                query = "select TRANSACTION_TYPE,TRANS_FROM,TRANS_TO,AMOUNT from transaction1 where CURR_DATE=#" + report_date.ToString() + "#;";
                OleDbCommand command = new OleDbCommand(query, conn);
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    string trans_type = reader[0].ToString();
                    string trans_from = reader[1].ToString();
                    string trans_to = reader[2].ToString();
                    int amt = int.Parse(reader[3].ToString());

                    Transaction_Class stock = new Transaction_Class(trans_type,trans_from,trans_to,amt);
                    transactionValues.Add(stock);

                }
                return "trans";
            }


            return "";




        }

        private void ConfigureCrystalReports()
        {

              string db_select=AddDBValues();
              Console.WriteLine(db_select);
          

            if (db_select.Equals("stock"))
            {
                if (stockValues.Count > 0)
                {
                    String reportPath = Path.GetFullPath("StockObject.rpt");
                    // String reportPath = "C:\\project\\project\\StockObject.rpt";
                    DBObjectsReport = new ReportDocument();
                    DBObjectsReport.Load(reportPath);
                    DBObjectsReport.SetDataSource(stockValues);
                    crystalReportViewer1.ReportSource = DBObjectsReport;
                }
              
            }
            else if (db_select.Equals("expense"))
            {
                if (expenseValues.Count > 0)
                {
                    String reportPath = Path.GetFullPath("ExpenseObject.rpt");
                    DBObjectsReport = new ReportDocument();
                    DBObjectsReport.Load(reportPath);
                    DBObjectsReport.SetDataSource(expenseValues);
                    crystalReportViewer1.ReportSource = DBObjectsReport;
                }
               
            }
            else if (db_select.Equals("trans"))
            {
                if (transactionValues.Count > 0)
                {
                    String reportPath = Path.GetFullPath("TransactionObject.rpt");
                    DBObjectsReport = new ReportDocument();
                    DBObjectsReport.Load(reportPath);
                    DBObjectsReport.SetDataSource(transactionValues);
                    crystalReportViewer1.ReportSource = DBObjectsReport;                    
                }
             
            }
            
        }

        private void Mag_Report_FormClosing(object sender, FormClosingEventArgs e)
        {
            rep_home.Close();
       
        }

    }
}

